package com.zyx.restaurant.server.common.exception;


import com.zyx.restaurant.server.common.constant.ResponseCode;
import com.zyx.restaurant.server.model.response.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>全局异常捕获
 *
 * @author Yaxi.Zhang
 * @since 2022/10/1 08:27
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public CommonResult<Void> invalidCommon(Exception ex) {
        log.error("全局异常捕获", ex);
        return new CommonResult<>(ResponseCode.RESPONSE_CODE_500, "全局异常捕获：" + ex.getMessage(), null);
    }

    @ResponseBody
    @ExceptionHandler(value = BusinessException.class)
    public CommonResult<Void> invalidBusiness(BusinessException ex) {
        log.error("全局异常捕获", ex);
        return new CommonResult<>(ResponseCode.RESPONSE_CODE_500, ex.getMessage(), null);
    }

}
